/**
 * Copyright 2010 upTick Pty Ltd
 *
 * Licensed under the terms of the GNU General Public License version 3 
 * as published by the Free Software Foundation. You may obtain a copy of the
 * License at: http://www.gnu.org/copyleft/gpl.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations 
 * under the License.
 */

package au.com.uptick.serendipity.client.sales.account.presenter;

import au.com.uptick.serendipity.client.NameTokens;
import au.com.uptick.serendipity.shared.action.RetrieveAccountAction;
import au.com.uptick.serendipity.shared.action.RetrieveAccountResult;
import au.com.uptick.serendipity.shared.dto.AccountDto;
import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.inject.Inject;
import com.gwtplatform.dispatch.shared.DispatchAsync;
import com.gwtplatform.mvp.client.Presenter;
import com.gwtplatform.mvp.client.View;
import com.gwtplatform.mvp.client.annotations.NameToken;
import com.gwtplatform.mvp.client.annotations.ProxyCodeSplit;
import com.gwtplatform.mvp.client.proxy.Place;
import com.gwtplatform.mvp.client.proxy.PlaceManager;
import com.gwtplatform.mvp.client.proxy.Proxy;
import com.gwtplatform.mvp.client.proxy.RevealContentEvent;

public class AccountInformationPresenter extends
                                         Presenter<AccountInformationPresenter.MyView, AccountInformationPresenter.MyProxy> {

    private final PlaceManager placeManager;
    private final DispatchAsync dispatcher;

    @ProxyCodeSplit
    @NameToken(NameTokens.accountInformation)
    public interface MyProxy extends Proxy<AccountInformationPresenter>, Place {
    }

    public interface MyView extends View {
        void setServerResponse(AccountDto account);
    }

    @Inject
    public AccountInformationPresenter(EventBus eventBus, MyView view, MyProxy proxy,
                                       PlaceManager placeManager, DispatchAsync dispatcher) {
        super(eventBus, view, proxy);

        this.placeManager = placeManager;
        this.dispatcher = dispatcher;
    }

    @Override
    protected void onBind() {
        super.onBind();

        Log.debug("onBind()");

        Long accountId = AccountPresenter.getAccountId();

        Log.debug("accountId == " + accountId);

        if (accountId == -1L) {
            accountId = 3L;
        }

        // get the data for the View's tabs
        dispatcher.execute(new RetrieveAccountAction(accountId),
                           new AsyncCallback<RetrieveAccountResult>() {
                               @Override
                               public void onFailure(Throwable caught) {
                                   Log.debug("onFailure() - " + caught.getMessage());
                               }

                               @Override
                               public void onSuccess(RetrieveAccountResult result) {
                                   Log.debug("onSuccess()");
                                   getView().setServerResponse(result.getAccountDto());
                               }
                           });
    }

    @Override
    protected void onUnbind() {
        super.onBind();

        Log.debug("onUnbind()");
    }

    @Override
    protected void onReveal() {
        super.onReveal();

        Log.debug("onReveal() - " + NameTokens.accountInformation);

        AccountPresenter.getNavigationPane().selectRecord(NameTokens.accountInformation);
    }

    @Override
    protected void onReset() {
        super.onReset();

        Log.debug("onReset()");
    }

    @Override
    protected void revealInParent() {
        RevealContentEvent.fire(this, AccountPresenter.TYPE_SetContextAreaContent, this);
    }
}